package javacourse.mq.activemq.activemq;

import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.stereotype.Service;

import javax.jms.ConnectionFactory;


/**
 * Create by chenhd on 2021/10/31.
 */
@Service
public class Consumer {

    //对于queue消息，多个消费者监听，一条消息只会有一个消费者拿到
    @JmsListener(destination = "test.queue")
    public void receiveQueueMsg(String msg) {
        System.out.println("------------------消费者1收到queue消息:" + msg);
    }

    @JmsListener(destination = "test.queue")
    public void receiveQueueMsg2(String msg) {
        System.out.println("------------------消费者2收到queue消息:" + msg);
    }

    //对于topic消息，多个消费者监听，一条消息所有消费者都能拿到
    @JmsListener(destination = "test.topic", containerFactory = "myJmsListenerContainerFactory")
    public void receiveTopicMsg(String msg) {
        System.out.println("------------------消费者1收到topic消息:" + msg);
    }

    @JmsListener(destination = "test.topic", containerFactory = "myJmsListenerContainerFactory")
    public void receiveTopicMsg2(String msg) {
        System.out.println("------------------消费者2收到topic消息:" + msg);
    }

    @Bean
    JmsListenerContainerFactory<?> myJmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }
}
